package com.app;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.app.net.NetHelper;
import com.app.util.Utils_Log;




public abstract class BaseFragment extends Fragment {

	protected FragmentActivity activity ;
	protected Toast mToast;
	protected View mMainView;
	protected RequestQueue mRequestQueue;
	protected LayoutInflater mInflater;

	protected String TAG = getClass().getSimpleName();
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		mInflater = inflater;
		mMainView = inflater.inflate( InitLayout() , null);
		return mMainView;
		
		
	}
	

	@Override
	public void onActivityCreated( Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		this.mRequestQueue = Volley.newRequestQueue(getActivity());

		activity = getActivity();

		InitView(mMainView);
		InitListener();
		InitProcess();
		
	}
	

	@Override
	public void onDestroyView() {
		super.onDestroyView();

		if (this.mRequestQueue != null)
			this.mRequestQueue.cancelAll( NetHelper.TAG_URL );

	};

	

	/**
	 * 根据输入文字提示信息
	 * 
	 * @param text
	 */
	public void ShowToast(final String text) {
		if (!TextUtils.isEmpty(text)) {
			(getActivity()).runOnUiThread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					if (mToast == null) {
						mToast = Toast.makeText(getActivity(), text,
								Toast.LENGTH_SHORT);
					} else {
						mToast.setText(text);
					}
					mToast.show();
				}
			});

		}
	}

	/**
	 * 根据ID提示信息
	 * 
	 * @param resId
	 */
	public void ShowToast(final int resId) {
		(getActivity()).runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (mToast == null) {
					mToast = Toast
							.makeText(getActivity().getApplicationContext(),
									resId, Toast.LENGTH_SHORT);
				} else {
					mToast.setText(resId);
				}
				mToast.show();
			}
		});

	}

	
	
	//////////////////////////////////////////////////////////////////////////////
	
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Utils_Log.d("activity_life", getClass().getName() + "----->start");
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Utils_Log.d("activity_life", getClass().getName() + "----->resume");
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Utils_Log.d("activity_life", getClass().getName() + "----->pause");
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Utils_Log.d("activity_life", getClass().getName() + "----->stop");
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Utils_Log.d("activity_life", getClass().getName() + "----->destroy");
	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		Utils_Log.d("activity_life", getClass().getName() + "----->onAttach");
	}
	
	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
		Utils_Log.d("activity_life", getClass().getName() + "----->onDetach");
	}
	
	
	public abstract Integer InitLayout();
	public abstract void InitView(View view);
	public abstract void InitListener();
	public abstract void InitProcess();
	


}









